<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="database.DAO, golf.Account, golf.ProductService"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	DAO dao = DAO.getInstance();
	Account account = null;
	String accountID = request.getParameter("id");
	if( (account = dao.login(accountID, request.getParameter("pw"))) != null ){
		ProductService ps = dao.getProductService(account.getPs_no());
		
		if( ps != null ){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			java.util.Calendar cal = java.util.Calendar.getInstance();
			
			cal.setTime(account.getPs_buy_date());
			cal.add(java.util.Calendar.DATE, ps.getPeriod() );
			
			int expiredDate = Integer.parseInt(sdf.format(cal.getTime()));
			int nowDate = Integer.parseInt(sdf.format(new java.util.Date()));
			if( expiredDate < nowDate ){ //요금상품 기간 만료처리. ACCOUNT에 요금상품번호를 삭제.
				dao.setProductService( accountID, 0 ); // 0을 넘기면 내부적으로 NULL로 바꿔서 초기화.
				System.out.println("요금상품 기간이 만료되었습니다.");
			}
		}
		
		
        session.setAttribute("accountID", account.getId());
        System.out.println("accountID(" +account.getId() + ")세션 추가 완료!");
	
		response.sendRedirect("../jsp/selectServer.jsp");
	}
	else{
		out.println("로그인이 정상적으로 처리되지 않았습니다!! 다시 시도해주세요.");
	}
%>
</body>
</html>